package com.youdoneed.community.forum.web;

import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.common.vo.ResultMessage;
import com.youdoneed.community.forum.entity.Forumnotice;
import com.youdoneed.community.forum.service.ForumNoticeService;
import com.youdoneed.community.forum.vo.ForumnoticeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 消息通知模块控制器
 *
 * @author liudong
 * @date 2017.7.12
 */
@SuppressWarnings("unchecked")
@RestController
@RequestMapping(value = "/forumNotice", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "消息通知模块控制器", description = "通知模块控制器")
public class ForumNoticeController {

    private static final Logger logger = LoggerFactory.getLogger(ForumNoticeController.class);

    @Autowired
    ForumNoticeService forumNoticeService;

    /**
     * @return ResultMessage
     * @author liudong
     * @date 2017.7.12
     */
    @ResponseBody
    @RequestMapping(value = "/getNoticeCnt", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查询通知数量", value = "查询通知数量")
    public ResultMessage<ForumnoticeVo> getNoticeCnt(@ApiParam(value = "客户端请求头附带参数") @RequestHeader(value = "Authorization") String token) {
        try {
            String userId = JWTUtils.getTokenUserId(token);
            ForumnoticeVo forumnoticeVo = forumNoticeService.qryFirstNotice(userId, null, Forumnotice.UN_READ);
            if (forumnoticeVo != null) {
                int cnt = forumNoticeService.qryNoticeCnt(userId, null, null, Forumnotice.UN_READ);
                forumnoticeVo.setLastNoticeCnt(cnt);
                forumnoticeVo.setQrySdate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            }
            return ResultMessage.newSuccess("查询成功").setData(forumnoticeVo);
        } catch (Exception e) {
            return ResultMessage.newFailure("查询失败");
        }
    }


    /**
     * @return ResultMessage
     * @author liudong
     * @date 2017.7.12
     */
    @ResponseBody
    @RequestMapping(value = "/qryNoticeList", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "查询通知列表", value = "查询通知列表")
    public ResultMessage<List<Forumnotice>> qryNoticeList(@ApiParam(value = "客户端请求头附带参数") @RequestHeader(value = "Authorization") String token,
                                                          @ApiParam(value = "0:未读；1:已读") Integer readStatus,
                                                          Integer currentPage, Integer rows) {
        try {
            String userId = JWTUtils.getTokenUserId(token);
            //消息通知，时间在查询时间以前
            List<Forumnotice> noticeList = forumNoticeService.qryNoticeList(userId, null, null, readStatus, currentPage, rows);
            return ResultMessage.newSuccess("查询成功").setData(noticeList);
        } catch (Exception e) {
            return ResultMessage.newFailure("查询失败");
        }

    }

    /**
     * @return ResultMessage
     * @author liudong
     * @date 2017.7.12
     */
    @ResponseBody
    @RequestMapping(value = "/updateNoticeStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(notes = "更新通知状态为已读", value = "更新通知状态为已读")
    public ResultMessage<Integer> updateNoticeList(@ApiParam(value = "通知id,不传只代表全部已读") @RequestParam(required = false) String forumNoticeId,
                                                   @ApiParam(value = "客户端请求头附带参数") @RequestHeader(value = "Authorization") String token) {
        try {

            String userId = JWTUtils.getTokenUserId(token);
            //消息通知，时间在查询时间以前
            Integer cnt = forumNoticeService.updateNoticeList(userId, forumNoticeId, null, null, Forumnotice.READED);

            return ResultMessage.newSuccess("操作成功");
        } catch (Exception e) {
            return ResultMessage.newFailure("操作失败");
        }

    }

    /**
     * @return ResultMessage
     * @author liudong
     * @date 2017.7.12
     */
    @ResponseBody
    @RequestMapping(value = "/saveNotice", method = {RequestMethod.POST})
    @ApiOperation(notes = "保存通知", value = "保存通知")
    public ResultMessage<Integer> saveNotice(@RequestBody Forumnotice forumNotice) {
        try {
            forumNotice.setForumNoticeId(StringUUIdUtil.getUUID());
            forumNoticeService.insertSelective(forumNotice);
            return ResultMessage.newSuccess("保存通知成功");
        } catch (Exception e) {
            return ResultMessage.newFailure("保存通知失败");
        }
    }

    @RequestMapping(value = "/saveNoticeInBatch", method = {RequestMethod.POST})
    @ApiOperation(notes = "批量保存加好友通知", value = "批量保存加好友通知")
    public ResultMessage<?> saveNoticeInBatch(String userId, @RequestBody List<String> friendIds) {
        forumNoticeService.saveNoticeInBatch(userId, friendIds);
        return  ResultMessage.newSuccess("批量保存加好友通知！");
    }
}
